package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void deleteCategory(Long id) {
        if(setMealMapper.countByCategory(id)!=0) { //当前分类关联了套餐，不能删除！
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        } else if(dishMapper.countByCategory(id)!=0) { //当前分类关联了菜品，不能删除！
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        } else if(categoryMapper.deleteById(id)==0) { //id对应分类找不到
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
    }

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> categoryList=categoryMapper.page(categoryPageQueryDTO);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.DISABLE);//套餐初始创建时，要禁用，否则前端会出现没有菜品的套餐
        categoryMapper.insertCategory(category);
    }

    @Override
    public List<Category> findByType(Integer type) {
        CategoryPageQueryDTO categoryPageQueryDTO=new CategoryPageQueryDTO();
        categoryPageQueryDTO.setType(type);
        return categoryMapper.page(categoryPageQueryDTO);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        if(categoryMapper.updateCategory(category)==0) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        Category category = Category.builder().id(id).status(status).build();
        if(categoryMapper.updateCategory(category)==0) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
    }


}
